import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/provide/cart_provide.dart';
import 'package:flutter_shop/provide/goods_list.dart';
import 'package:provide/provide.dart';
import '../model/category_page_data.dart';
import 'package:fluttertoast/fluttertoast.dart';


class SideNavbar extends StatefulWidget {
  @override
  _SideNavbarState createState() => _SideNavbarState();
}

class _SideNavbarState extends State<SideNavbar> {
  List list = [];
  String _currentNavItemId;

  @override
  void initState() {
    super.initState();

    _currentNavItemId = '0001';
    list = CategoryDataListModel.fromJson(CategoryData).data;

    WidgetsBinding.instance.addPostFrameCallback((_) {
      getCartSubNavListByProvider();
    });
  }

  void changeCurrentNavItemId (BuildContext context, String index) {
    getCartSubNavListByProvider(index);

    if (!index.startsWith('000')) {
      Fluttertoast.showToast(
        msg: "测试按钮",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
      );
    }

    setState(() {
      this._currentNavItemId = index;
    });
  }

  void getCartSubNavListByProvider ([String id='0001']) {
    Provide.value<CartSubNavList>(context).getCartSubNavList(id: id);
    Provide.value<GoodsProvider>(context).getGoodsList(id: id);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(180.0),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(right: BorderSide(width: 1.0, color: Colors.black45))
      ),

      child: ListView.builder(
        itemCount: list.length,
        itemBuilder: (context, index) {
          return SideNavbarItem(onChange: changeCurrentNavItemId, item: list[index], isCurrent: _currentNavItemId == list[index].mallCategoryId);
        }
      ),
    );
  }
}

class SideNavbarItem extends StatelessWidget {
  final CategoryDataModel item;
  final onChange;
  final bool isCurrent;
  const SideNavbarItem({this.onChange, Key key, this.item, this.isCurrent }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    void _changeIndex (String id) {
      this.onChange(context, id);
    }

    return InkWell(
      onTap: () {
        _changeIndex(item.mallCategoryId);
      },
      child: Container(
        width: ScreenUtil().setWidth(180.0),
        height: ScreenUtil().setHeight(100.0),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(width: 1.0, color: Colors.black45)
          ),
          color: isCurrent ? Color(0x3d000000) : Colors.white
        ),

        child: Align(
          alignment: Alignment(-0.8, 0),
          child: Text(item.mallCategoryName, style: TextStyle(
              fontSize: ScreenUtil().setSp(28.0),
              color: Color(0xff333333),
            )
          ),
        ),
      ),
    );
  }
}